package com.sum.tea.ui

import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.annotation.StringRes
import com.sum.common.bean.ReadParams
import com.sum.common.bean.WriteParams
import com.sum.tea.base.BaseMvvmFragment
import com.sum.common.bean.CommonResult
import com.sum.tea.R
import com.sum.tea.databinding.ActivityReadwriteBinding
import com.sum.tea.viewmodel.ReadwriteViewModel

class ReadwriteFragment : BaseMvvmFragment<ActivityReadwriteBinding, ReadwriteViewModel>() {

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initSpinner()
        initClickListeners()
        observeViewModel()
    }
    private fun initSpinner() {
        val regions = resources.getStringArray(R.array.spibank)
        val adapter = ArrayAdapter(
            requireContext(),
            android.R.layout.simple_spinner_item,
            regions
        ).apply {
            setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        }

        mBinding.snRegion.apply {
            setAdapter(adapter)
            setSelection(1)
        }
    }

    private fun initClickListeners() {
        with(mBinding) {
            this.btnReadlabel.setOnClickListener { handleReadLabel() }
            this.btnWritelabel.setOnClickListener { handleWriteLabel() }
        }
    }

    private fun handleReadLabel() {
        with(mBinding) {
            val params = ReadParams(
                this.etPassword.text.toString(),
                this.snRegion.selectedItemPosition,
                Integer.valueOf(this.etStrataddress.text.toString()),
                Integer.valueOf(this.etDatelength.text.toString())
            )
            mViewModel.readLabel(params)
        }
    }

    private fun handleWriteLabel() {
        with(mBinding) {
            val params = WriteParams(
                this.etPassword.text.toString(),
                this.snRegion.selectedItemPosition,
                Integer.valueOf(this.etStrataddress.text.toString()),
                this.etReaddata.text.toString(),
                this.etWritedata.text.toString()
            )
            mViewModel.writeLabel(params)
        }
    }

    private fun observeViewModel() {
        mViewModel.readResult.observe(viewLifecycleOwner) { result ->
            when (result) {
                is CommonResult.Success -> {
                    if (result.data != mBinding?.etReaddata?.text.toString()) {
                        mBinding?.etReaddata?.setText(result.data)
                        mBinding?.etWritedata?.setText(result.data)
                        showToast(R.string.read_ok)
                    }
                }
                is CommonResult.Error -> showToast(R.string.read_failed)
                is CommonResult.Exception -> showToast(getString(R.string.Constr_GetFaill) + result.message)
            }
        }

        mViewModel.writeResult.observe(viewLifecycleOwner) { result ->
            when (result) {
                is CommonResult.Success -> {
                    showToast(R.string.write_ok)
                }
                is CommonResult.Error -> showToast(R.string.write_failed)
                is CommonResult.Exception -> showToast(getString(R.string.abnormal) + result.message)
            }
        }
    }
    private fun showToast(@StringRes messageRes: Int) {
        Toast.makeText(requireContext(), messageRes, Toast.LENGTH_SHORT).show()
    }

    private fun showToast(message: String) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
    }
}